/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IngresoUsuario.java
 *
 * Created on 22/06/2011, 07:58:03 PM
 */

package guiKiosco;

import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import logica.Fabrica;
import logica.IKiosco;
import util.datatypes.DTFuncion;
import util.datatypes.DTReserva;
import util.datatypes.DTTiposEntrada;

/**
 *
 * @author bruno
 */
public class IngresoUsuario extends javax.swing.JFrame {

    private double costoTotal;
    private DTFuncion funcion;
    private List<DTTiposEntrada> tipoEntrada;
    
    public IngresoUsuario() {
        
        initComponents();

        this.setLocationRelativeTo(null);
        this.setSize(1024,768);
    }

    public IngresoUsuario(double cTotal, DTFuncion func, List<DTTiposEntrada> tEntrada) {

        initComponents();
        
        this.setLocationRelativeTo(null);
        this.setSize(1024,768);

        costoTotal = cTotal;
        funcion = func;
        tipoEntrada = tEntrada;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pSuperior = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnAceptar = new javax.swing.JButton();
        txtCedula = new javax.swing.JTextField();
        btnAtras = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(1024, 768));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 36));
        jLabel1.setText("Ingrese su cedula para confirmar su reserva");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/guiKiosco/resources/avatar.png"))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Arial", 0, 14));
        jLabel3.setText("Usted debe estar registrado para poder hacer reservas");

        javax.swing.GroupLayout pSuperiorLayout = new javax.swing.GroupLayout(pSuperior);
        pSuperior.setLayout(pSuperiorLayout);
        pSuperiorLayout.setHorizontalGroup(
            pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pSuperiorLayout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addContainerGap(72, Short.MAX_VALUE))
        );
        pSuperiorLayout.setVerticalGroup(
            pSuperiorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pSuperiorLayout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(69, 69, 69))
            .addGroup(pSuperiorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                .addGap(23, 23, 23))
        );

        getContentPane().add(pSuperior, java.awt.BorderLayout.PAGE_START);

        btnAceptar.setBackground(new java.awt.Color(242, 241, 240));
        btnAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/guiKiosco/resources/search.png"))); // NOI18N
        btnAceptar.setBorder(null);
        btnAceptar.setBorderPainted(false);
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        txtCedula.setFont(new java.awt.Font("Arial", 1, 36));

        btnAtras.setBackground(new java.awt.Color(242, 241, 240));
        btnAtras.setIcon(new javax.swing.ImageIcon(getClass().getResource("/guiKiosco/resources/arrow_left.png"))); // NOI18N
        btnAtras.setBorder(null);
        btnAtras.setBorderPainted(false);
        btnAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAtrasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(318, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAceptar)
                        .addGap(297, 297, 297))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnAtras)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(146, 146, 146)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(16, 16, 16)
                        .addComponent(txtCedula, javax.swing.GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE)
                        .addGap(9, 9, 9)))
                .addGap(209, 209, 209)
                .addComponent(btnAtras)
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

        try {

            IKiosco i = Fabrica.getIKiosco();

            int cedula = Integer.parseInt(txtCedula.getText());

            if(i.validarCliente(cedula)){

                if (!i.chequearFuncionesSolapadas(funcion, cedula)) {

                    Date fecha = new Date();
                    DTReserva reserva = new DTReserva(fecha, costoTotal, false, false, funcion, tipoEntrada);

                    int retorno = i.agregarReservaCliente(reserva, cedula);

                    if(retorno == 1){

                        JOptionPane.showMessageDialog(null, "Su reserva fue ingresada correctamente, pase por caja para retirar las entradas");

                        ListaPeliculas frm = new ListaPeliculas();
                        frm.setVisible(true);

                        this.setVisible(false);

                    }else{

                        JOptionPane.showMessageDialog(null, "Error al ingresar la reserva, vuelva a intentarlo mas tarde");

                    }

                }else{

                    JOptionPane.showMessageDialog(null, "Usted tiene reservas que se superpone con esta funcion");

                }

            }else{

                JOptionPane.showMessageDialog(null, "No existe el usuario o usted es menor de 12 años");

            }

        } catch (Exception ex) {

            JOptionPane.showMessageDialog(null, "Debe ingresar la cedula sin letra ni guiones");
            
        }

    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAtrasActionPerformed

        IKiosco i = Fabrica.getIKiosco();

        ListaFunciones frm = new ListaFunciones(i.getFuncionPelicula(funcion));
        frm.setVisible(true);

        this.setVisible(false);
    }//GEN-LAST:event_btnAtrasActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IngresoUsuario().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnAtras;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pSuperior;
    private javax.swing.JTextField txtCedula;
    // End of variables declaration//GEN-END:variables

}
